package com.weayer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weayer.constant.CommonStatusEnum;
import com.weayer.constant.DriverConstants;
import com.weayer.dto.DriverCarBindingRelationship;
import com.weayer.dto.DriverUser;
import com.weayer.dto.ResponseResult;
import com.weayer.mapper.DriverCarBindingRelationshipMapper;
import com.weayer.mapper.DriverUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author Weayer
 * @since 2024-03-01
 */
@Service
public class DriverCarBindingRelationshipService{
    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;
    @Autowired
    DriverUserMapper driverUserMapper;

    /**
     * @Description: 司机和车辆关系绑定
     * @Param: [driverCarBindingRelationship]
     * @return: com.weayer.dto.ResponseResult
     **/
    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship){
        // 判断车辆和司机是否已经绑定过
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        queryWrapper.eq("bind_state", DriverConstants.DRIVER_CAR_BIND);
        Integer flag = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if(flag.intValue() > 0){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getCode(), CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getValue(), driverCarBindingRelationship);
        }
        // 判断车辆是否已经绑定过
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        queryWrapper.eq("bind_state", DriverConstants.DRIVER_CAR_BIND);
        flag = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if(flag.intValue() > 0){
            return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXISTS.getCode(), CommonStatusEnum.CAR_BIND_EXISTS.getValue(), driverCarBindingRelationship);
        }
        // 判断司机是否已经绑定过
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        queryWrapper.eq("bind_state", DriverConstants.DRIVER_CAR_BIND);
        flag = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if(flag.intValue() > 0){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXISTS.getCode(), CommonStatusEnum.DRIVER_BIND_EXISTS.getValue(), driverCarBindingRelationship);
        }

        LocalDateTime now = LocalDateTime.now();
        driverCarBindingRelationship.setBindingTime(now);
        driverCarBindingRelationship.setBindState(DriverConstants.DRIVER_CAR_BIND);
        driverCarBindingRelationshipMapper.insert(driverCarBindingRelationship);
        return ResponseResult.success("");
    }
    /**
     * @Description: 司机和车辆关系解绑
     * @Param: [driverCarBindingRelationship]
     * @return: com.weayer.dto.ResponseResult
     **/
    public ResponseResult unbind(DriverCarBindingRelationship driverCarBindingRelationship){
        LocalDateTime now = LocalDateTime.now();
        // 默认车辆和司机存在过绑定关系
        // 判断车辆和司机是否已经绑定过
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        queryWrapper.eq("bind_state", DriverConstants.DRIVER_CAR_BIND);
        Integer flag = driverCarBindingRelationshipMapper.selectCount(queryWrapper);
        if(flag.intValue() == 0){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(), CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue(), driverCarBindingRelationship);
        }

        driverCarBindingRelationship.setBindState(DriverConstants.DRIVER_CAR_UNBIND);
        driverCarBindingRelationship.setUnBindingTime(now);
        driverCarBindingRelationshipMapper.updateById(driverCarBindingRelationship);

        return ResponseResult.success("");
    }
    /**
     * @Description: 根据driverPhone获取车辆和司机关系
     * @Param: [driverPhone]
     * @return: com.weayer.dto.ResponseResult<com.weayer.dto.DriverCarBindingRelationship>
     **/
    public ResponseResult<DriverCarBindingRelationship> getDriverCarRelationShip(String driverPhone){
        QueryWrapper<DriverUser> queryDriverWrapper = new QueryWrapper();
        queryDriverWrapper.eq("driver_phone", driverPhone);
        DriverUser driverUser = driverUserMapper.selectOne(queryDriverWrapper);
        Long driverId = driverUser.getId();

        QueryWrapper<DriverCarBindingRelationship> queryRelationShipMapper = new QueryWrapper();
        queryRelationShipMapper.eq("driver_id",driverId);
        queryRelationShipMapper.eq("bind_state",DriverConstants.DRIVER_CAR_BIND);
        return ResponseResult.success(driverCarBindingRelationshipMapper.selectOne(queryRelationShipMapper));
    }
}
